package com.edu.exam.entity.quality.monitoring;

import com.edu.exam.enums.system.RiskLevel;
import com.edu.exam.enums.quality.monitoring.PermissionType;
import com.edu.exam.enums.quality.monitoring.PermissionLevel;
import com.edu.exam.enums.quality.monitoring.ScopeType;
import com.edu.exam.enums.quality.monitoring.PermissionStatus;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;

/**
 * 分配权限实体类
 *
 * 根据E-06-01任务分配子模块设计实现
 * 对应allocation_permission数据表，用于存储用户的分配操作权限
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
public class AllocationPermission {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "user_id", length = 50, nullable = false)
    private String userId;

    @Enumerated(EnumType.STRING)
    @Column(name = "permission_type", length = 50, nullable = false)
    private PermissionType permissionType;

    @Enumerated(EnumType.STRING)
    @Column(name = "permission_level", length = 20, nullable = false)
    private PermissionLevel permissionLevel;

    @Enumerated(EnumType.STRING)
    @Column(name = "scope_type", length = 20)
    private ScopeType scopeType;

    @Column(name = "scope_id", length = 50)
    private String scopeId;

    @Column(name = "granted_by", length = 50, nullable = false)
    private String grantedBy;

    @Column(name = "granted_time", nullable = false)
    private LocalDateTime grantedTime;

    @Column(name = "expires_time")
    private LocalDateTime expiresTime;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 20, nullable = false)
    private PermissionStatus status;

    @Column(name = "remarks", columnDefinition = "TEXT")
    private String remarks;

    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Column(name = "update_time", nullable = false)
    private LocalDateTime updatedTime;

    // 扩展字段
    @Column(name = "grant_reason", columnDefinition = "TEXT")
    private String grantReason; // 授权原因

    @Column(name = "usage_count")
    private Integer usageCount; // 使用次数

    @Column(name = "last_used_time")
    private LocalDateTime lastUsedTime; // 最后使用时间

    @Column(name = "max_usage_count")
    private Integer maxUsageCount; // 最大使用次数限制

    @Column(name = "approval_required")
    private Boolean approvalRequired; // 是否需要审批

    @Column(name = "approved_by", length = 50)
    private String approvedBy; // 审批人

    @Column(name = "approval_time")
    private LocalDateTime approvalTime; // 审批时间

    @Column(name = "approval_comments", columnDefinition = "TEXT")
    private String approvalComments; // 审批意见

    @Column(name = "condition_rules", columnDefinition = "TEXT")
    private String conditionRules; // 条件规则（JSON格式）

    @Column(name = "restriction_rules", columnDefinition = "TEXT")
    private String restrictionRules; // 限制规则（JSON格式）

    @Column(name = "temporary_access")
    private Boolean temporaryAccess; // 是否为临时权限

    @Column(name = "emergency_access")
    private Boolean emergencyAccess; // 是否为紧急访问权限

    @Column(name = "audit_required")
    private Boolean auditRequired; // 是否需要审计

    @Column(name = "notification_settings", columnDefinition = "TEXT")
    private String notificationSettings; // 通知设置（JSON格式）

    @Column(name = "auto_renew")
    private Boolean autoRenew; // 是否自动续期

    @Column(name = "renewal_days_before")
    private Integer renewalDaysBefore; // 提前续期天数

    @Column(name = "last_notification_time")
    private LocalDateTime lastNotificationTime; // 最后通知时间
}